﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace htmltortf
{
    public partial class Form1 : Form
    {
        public static int filen;
        public static ArrayList files = new ArrayList();
        public Form1()
        {
            InitializeComponent();
        }

        private void btnConverte_Click(object sender, EventArgs e)
        {
            webBrowser1.Document.ExecCommand("SelectAll", false, null);
            webBrowser1.Document.ExecCommand("Copy", false, null);
            richTextBox1.Paste();
            string[] lines = richTextBox1.Lines;
            //lines[0].Remove(0);
            richTextBox1.ResetText();
            for (int i = 1; i < lines.Length - 2; i++)
            {
                richTextBox1.Text += lines[i] + "\r\n";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            filen = 0;
            //webBrowser1.Navigate(new Uri("http://localhost:8080/Biblia_HTML/01Gene01.htm"));        
            DirectoryInfo di = new DirectoryInfo(@"C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\Biblia_HTML");//"C:\Users\Ailton\Documents\igreja\Biblia_HTML\");
            FileInfo[] file = di.GetFiles();
            for (int i = 0; i < file.Length; i++)
            {
                files.Add(file[i].Name);
                //StreamReader fstr = file[i].OpenText();
                //Stream str = fstr.Read();
                //  webBrowser1.DocumentText=fstr.ReadToEnd();
                //webBrowser1.DocumentStream = fstr.;   
                //while ( webBrowser1.DocumentText != str.ToString()){
                //  Application.DoEvents();
                //}
                //webBrowser1.Navigate(new Uri("http://localhost:8080/Biblia_HTML/"+ file[i].Name));
                //webBrowser1.Refresh(WebBrowserRefreshOption.Completely);
                //webBrowser1.Document.ExecCommand("SelectAll", false, null);
                //webBrowser1.Document.ExecCommand("Copy", false, null);

                //richTextBox1.ResetText();
                //richTextBox1.Paste();
            }
            //carrega();
        }

        private void tmrCoversao_Tick(object sender, EventArgs e)
        {
            try
            {
                tmrCoversao.Enabled = false;
                //tmrRTF.Enabled = true;
            }
            catch { tmrCoversao.Enabled = false; }//tmrRTF.Enabled = false; }
        }
        public void carrega()
        {
            webBrowser1.Navigate(new Uri("http://localhost:8080/Biblia_HTML/" + files[filen]));
            webBrowser1.Refresh(WebBrowserRefreshOption.Completely);
            filen += 1;
              
        }
        private void tmrRTF_Tick(object sender, EventArgs e)
        {
            
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.Document != null)
            {
                webBrowser1.Document.ExecCommand("SelectAll", false, null);
                webBrowser1.Document.ExecCommand("Copy", false, null);

                richTextBox1.ResetText();
                richTextBox1.Paste();
                string[] lines = richTextBox1.Lines;
                richTextBox1.ResetText();
                for (int i = 1; i < lines.Length - 2; i++)
                {
                    richTextBox1.Text += lines[i] + "\r\n";
                }
                igrejaTableAdapters.CapituloTableAdapter cta = new htmltortf.igrejaTableAdapters.CapituloTableAdapter();
                igrejaTableAdapters.LivrosTableAdapter lta = new htmltortf.igrejaTableAdapters.LivrosTableAdapter();
                igreja.LivrosDataTable ldt = lta.GetDataBySiglaconv("Levi");//.GetDataBySiglaconv(files[filen].ToString().Substring(2));
                int livroid = 0;
                int capitulo = 0;
                for (int i = 0; i < ldt.Count; i++)
                {
                    if (files[filen].ToString().Contains(ldt.Rows[i]["siglaconversao"].ToString()))
                    {
                        livroid = Convert.ToInt32(ldt.Rows[i]["livro_id"]);
                        string[] separador = { ldt.Rows[i]["siglaconversao"].ToString(), "." };
                        string[] arquivo = files[filen].ToString().Split(separador, StringSplitOptions.RemoveEmptyEntries);
                        capitulo = Convert.ToInt32(arquivo[1]);
                        break;
                    }
                }
                if (livroid > 0)
                {
                    CapitulosTableAdapters.CapituloTableAdapter cap = new htmltortf.CapitulosTableAdapters.CapituloTableAdapter();
                    cap.Insert(capitulo, richTextBox1.Text, livroid);
                    //cta.Insert(capitulo, richTextBox1.Text.ToString(), livroid);
                }
                cta.Dispose();
                lta.Dispose();
                ldt.Dispose();
            }
           // tmrRTF.Enabled = false;
            //tmrCoversao.Enabled = true;
            carrega();

        }

        private void btnBiblia_Click(object sender, EventArgs e)
        {
            Master mform = new Master();
            mform.Show();
            this.Hide();
        }
        //Dim reportWebBrowser As New WebBrowser
        //    reportWebBrowser.CreateControl()
        //    reportWebBrowser.DocumentText = sbHTMLDoc.ToString 
        //        While reportWebBrowser.DocumentText <> sbHTMLDoc.ToString 
        //            Application.DoEvents() 
        //            End While
        //                reportWebBrowser.Document.ExecCommand("SelectAll", False, Nothing)  
        //reportWebBrowser.Document.ExecCommand("Copy", False, Nothing)   
        //Dim reportRichTextBox As New RichTextBox   
        //    reportRichTextBox.Paste()    
        //    reportRichTextBox.SaveFile(DocumentFileName) 
    }
}
